package Tests;

import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumEditTest extends AutoTestUtils{
    @BeforeAll
    public static void setUpBeforeClass() throws Exception {
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//        //打开博客登录页面
//        webDriver.get("http://127.0.0.1:9580/sign-in.html");
//        // 在每次登录之后都要进行清空，然后才能重新输入
//        webDriver.findElement(By.cssSelector("#username")).clear();
//        webDriver.findElement(By.cssSelector("#password")).clear();
//
//        //输入长高admin
//        webDriver.findElement(By.cssSelector("#username")).sendKeys("张三");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//        //输入密码123
//        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//        //提交登录
//        webDriver.findElement(By.cssSelector("#submit")).click();

        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //打开论坛首页页面
        webDriver.get("http://127.0.0.1:9580/index.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击发布帖子
        webDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.body.style.zoom='50%'");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

     /*
     * 测试编辑页可以正常打开
     */
    @Test
    @Order(1)
    void editPageLoadRight() {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 可以多检查几个，确保正确
        webDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none"));
        webDriver.findElement(By.cssSelector("body > div.page > header:nth-child(2)"));
        webDriver.findElement(By.cssSelector("#page-wrapper"));

    }
    /*
     *发布帖子
     */
    @Order(2)
    @Test
    void PostMessage() throws InterruptedException {
        //打开论坛首页
        webDriver.get("http://127.0.0.1:9580/index.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击发布帖子
        webDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.body.style.zoom='50%'");
        //选择板块"灌水区"
        webDriver.findElement(By.cssSelector("#article_post_borad")).click();
        webDriver.findElement(By.cssSelector("#article_post_borad > option:nth-child(9)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //将标题输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"article_post_title\").value=\"自动化测试\"");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //编辑帖子内容, tab键跳转到输入内容的位置
        webDriver.findElement(By.cssSelector("#article_post_title")).sendKeys(Keys.TAB);
        webDriver.switchTo().activeElement().sendKeys("自动化测试成功");
        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击发布按钮
        webDriver.findElement(By.cssSelector("#article_post_submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(1000);
        //获取当前url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:9580/index.html", cur_url);
        sleep(3000);
    }

    /*
     *校验已发布博客标题和发布时间
     */
    @Order(3)
    @Test
    void ForumInfoCheck() throws InterruptedException {
        //获取第一篇帖子的标题
        String cur_title = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //检查是否与刚才发布的帖子标题一致
        Assertions.assertEquals("自动化测试", cur_title);
        sleep(3000);
        //获取第一篇帖子的发布时间
        String  cur_time = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[2]")).getText();
        //检查是否与刚才发布的帖子时间一致
        Date date = new Date();
        SimpleDateFormat this_time = new SimpleDateFormat("yyyy-MM-dd");
        if(cur_time.contains(this_time.format(date))){
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");
        }
        sleep(3000);
    }
}
